﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.FireProjects.Model;
using Yipusoft.FireProjects.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;


public partial class BasicBase_Drilling_DrillingNoteNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {
        //部门梆定
        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        if (userInfo.DepartmentID.Substring(0, 4) == "1300")
        {
            DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
            DepartmentList.DataBind();
            DepartmentListBind();
        }
        else
        {
            DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
            DepartmentList.DataBind();
            DepartmentListBind();
        }

        if (Request.QueryString["ProjectID"] != null)
        {
            FireProjectInfo fireProject = FireProject.getFireproject(new Guid(Request.QueryString["ProjectID"]));
            pjc.Text = fireProject.ProjectName;
            hfpjcId.Value = Request.QueryString["ProjectID"];
        }
        if (Request.QueryString["id"] != null)
        {
            ActionTitle.Text = "修改演练记录";
            if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201312") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201314"))
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            DrillingNoteInfo info = DrillingNote.GetDetail(new Guid(Request.QueryString["id"]));
            CircsSeting.Text = info.CircsSeting;
            Compare.Text = info.Compare;
            DateDrilling.Text = info.DateDrilling.ToShortDateString();
            DepartmentList.SelectedValue = info.DepartmentId.Substring(0, 4) + "000";
            UnitList.SelectedValue = info.DepartmentId;
            Effect.Text = info.Effect;
            hfpjcId.Value = info.FireProjectID.ToString();
            FireProjectInfo fireProject = FireProject.getFireproject(info.FireProjectID);
            pjc.Text = fireProject.ProjectName;
            Note.Text = info.Note;
            Part.Text = info.Part;
            Principal.Text = info.Principal;
            SetAd.Text = info.SetAd;
            Wind.Text = info.Wind;
            Place.Text = info.Place;
            ID.Text = Request.QueryString["id"];
        }
        else
        {
            ActionTitle.Text = "添加演练记录";
            if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201312") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "201313"))
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
            btnDepart.Visible = false;
        }
    }

    void DepartmentListBind()
    {
        string strWhere = "(DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' ) or departmentid='" + DepartmentList.SelectedValue + "'";
        UnitList.DataSource = Department.GetList(strWhere);
        UnitList.DataBind();
    }

    protected void DepartmentList_TextChanged(object sender, EventArgs e)
    {
        DepartmentListBind();
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (ID.Text != "")
        {
            //修改
            DrillingNoteInfo info = DrillingNote.GetDetail(new Guid(ID.Text));
            info.CircsSeting = CircsSeting.Text;
            info.Compare = Compare.Text;
            info.DateDrilling = Convert.ToDateTime(DateDrilling.Text);
            info.DepartmentId = UnitList.SelectedValue;
            info.Effect = Effect.Text;
            info.FireProjectID = new Guid(hfpjcId.Value);
            info.Note = Note.Text;
            info.Part = Part.Text;
            info.Principal = Principal.Text;
            info.SetAd = SetAd.Text;
            info.TimeLastModified = DateTime.Now;
            info.Wind = Wind.Text;
            info.Place = Place.Text;

            if (DrillingNote.Update(info) > 0)
            {
                ///添加完成.
                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "修改一个演练记录";
                log.ItemID = Request.QueryString["id"];
                log.ItemName = "BasicBase";
                log.ItemFullUrl = "Drilling/DrillingNew.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);
                Prompt.Text = "提示:保存成功!";


            }
            else
            {
                Prompt.Text = "提示:保存失败!";
            }

        }
        else
        {
            //添加
            DrillingNoteInfo info = new DrillingNoteInfo();
            info.CircsSeting = CircsSeting.Text;
            info.Compare = Compare.Text;
            info.DateDrilling = Convert.ToDateTime(DateDrilling.Text);
            info.DepartmentId = UnitList.SelectedValue;
            info.Effect = Effect.Text;
            info.FireProjectID = new Guid(hfpjcId.Value);
            info.Note = Note.Text;
            info.Part = Part.Text;
            info.Principal = Principal.Text;
            info.SetAd = SetAd.Text;
            info.TimeLastModified = DateTime.Now;
            info.Wind = Wind.Text;
            info.Place = Place.Text;
            Guid id = DrillingNote.Insert(info);
            if (id != Guid.Empty)
            {
                ///添加完成.
                EventLogInfo log = new EventLogInfo();
                log.EventTime = DateTime.Now;
                log.EventType = 1;
                log.EventData = "添加一个演练记录";
                log.ItemID = id.ToString();
                log.ItemName = "BasicBase";
                log.ItemFullUrl = "Drilling/DrillingNew.aspx";
                log.CreatedBy = Context.User.Identity.Name;
                log.CreatedTime = DateTime.Now;
                log.UserIP = Request.UserHostAddress;
                EventLog.Insert(log);
                Prompt.Text = "提示:保存成功!";
                ID.Text = id.ToString();
                btnDepart.Visible = true;
            }
            else
            {
                Prompt.Text = "提示:保存失败!";
            }

        }
    }

    protected void department_Click(object sender, EventArgs e)
    {
        Response.Redirect("DrillingDepartment.aspx?id=" + ID.Text);
    }
}
